<template>
  <div aria-label="Orange and tan hamster running in a metal wheel" role="img" class="wheel-and-hamster">
    <div class="wheel"></div>
    <div class="hamster">
      <div class="hamster__body">
        <div class="hamster__head">
          <div class="hamster__ear"></div>
          <div class="hamster__eye"></div>
          <div class="hamster__nose"></div>
        </div>
        <div class="hamster__limb hamster__limb--fr"></div>
        <div class="hamster__limb hamster__limb--fl"></div>
        <div class="hamster__limb hamster__limb--br"></div>
        <div class="hamster__limb hamster__limb--bl"></div>
        <div class="hamster__tail"></div>
      </div>
    </div>
    <div class="spoke"></div>
  </div>
</template>

<script setup lang="ts">
  const props = defineProps({
    scale: {
      type: String,
      default: '0.5',
    },
  });

  const sizeFn = (defaultSize) => defaultSize * Number(props.scale) + 'em';
</script>

<style lang="scss">
  .wheel-and-hamster {
    --dur: 1s;
    position: relative;
    width: v-bind(sizeFn(12));
    height: v-bind(sizeFn(12));
    font-size: 14px;
    transform: scale(v-bind(scale));
  }

  .wheel,
  .hamster,
  .hamster div,
  .spoke {
    position: absolute;
  }

  .wheel,
  .spoke {
    border-radius: 50%;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  .wheel {
    background: radial-gradient(100% 100% at center, hsla(0, 0%, 60%, 0) 47.8%, hsl(0, 0%, 60%) 48%);
    z-index: 2;
  }

  .hamster {
    animation: hamster var(--dur) ease-in-out infinite;
    top: 50%;
    left: calc(50% - v-bind(sizeFn(3.5)));
    width: v-bind(sizeFn(7));
    height: v-bind(sizeFn(3.75));
    transform: rotate(4deg) translate(v-bind(sizeFn(-0.8)), v-bind(sizeFn(1.85)));
    transform-origin: 50% 0;
    z-index: 1;
  }

  .hamster__head {
    animation: hamsterHead var(--dur) ease-in-out infinite;
    background: hsl(30, 90%, 55%);
    border-radius: 70% 30% 0 100% / 40% 25% 25% 60%;
    box-shadow:
      0 v-bind(sizeFn(-0.25)) 0 hsl(30, 90%, 80%) inset,
      v-bind(sizeFn(0.75)) v-bind(sizeFn(-1.55)) 0 hsl(30, 90%, 90%) inset;
    top: 0;
    left: v-bind(sizeFn(-2));
    width: v-bind(sizeFn(2.75));
    height: v-bind(sizeFn(2.5));
    transform-origin: 100% 50%;
  }

  .hamster__ear {
    animation: hamsterEar var(--dur) ease-in-out infinite;
    background: hsl(0, 90%, 85%);
    border-radius: 50%;
    box-shadow: v-bind(sizeFn(-0.25)) 0 hsl(30, 90%, 55%) inset;
    top: v-bind(sizeFn(-0.25));
    right: v-bind(sizeFn(-0.25));
    width: v-bind(sizeFn(0.75));
    height: v-bind(sizeFn(0.75));
    transform-origin: 50% 75%;
  }

  .hamster__eye {
    animation: hamsterEye var(--dur) linear infinite;
    background-color: hsl(0, 0%, 0%);
    border-radius: 50%;
    top: v-bind(sizeFn(0.375));
    left: v-bind(sizeFn(1.25));
    width: v-bind(sizeFn(0.5));
    height: v-bind(sizeFn(0.5));
  }

  .hamster__nose {
    background: hsl(0, 90%, 75%);
    border-radius: 35% 65% 85% 15% / 70% 50% 50% 30%;
    top: v-bind(sizeFn(0.75));
    left: 0;
    width: v-bind(sizeFn(0.2));
    height: v-bind(sizeFn(0.25));
  }

  .hamster__body {
    animation: hamsterBody var(--dur) ease-in-out infinite;
    background: hsl(30, 90%, 90%);
    border-radius: 50% 30% 50% 30% / 15% 60% 40% 40%;
    box-shadow:
      v-bind(sizeFn(0.1)) v-bind(sizeFn(0.75)) 0 hsl(30, 90%, 55%) inset,
      v-bind(sizeFn(0.15)) v-bind(sizeFn(-0.5)) 0 hsl(30, 90%, 80%) inset;
    top: v-bind(sizeFn(0.25));
    left: v-bind(sizeFn(2));
    width: v-bind(sizeFn(4.5));
    height: v-bind(sizeFn(3));
    transform-origin: 17% 50%;
    transform-style: preserve-3d;
  }

  .hamster__limb--fr,
  .hamster__limb--fl {
    clip-path: polygon(0 0, 100% 0, 70% 80%, 60% 100%, 0% 100%, 40% 80%);
    top: v-bind(sizeFn(2));
    left: v-bind(sizeFn(0.5));
    width: v-bind(sizeFn(1));
    height: v-bind(sizeFn(1.5));
    transform-origin: 50% 0;
  }

  .hamster__limb--fr {
    animation: hamsterFRLimb var(--dur) linear infinite;
    background: linear-gradient(hsl(30, 90%, 80%) 80%, hsl(0, 90%, 75%) 80%);
    transform: rotate(15deg) translateZ(-1px);
  }

  .hamster__limb--fl {
    animation: hamsterFLLimb var(--dur) linear infinite;
    background: linear-gradient(hsl(30, 90%, 90%) 80%, hsl(0, 90%, 85%) 80%);
    transform: rotate(15deg);
  }

  .hamster__limb--br,
  .hamster__limb--bl {
    border-radius: v-bind(sizeFn(0.75)) v-bind(sizeFn(0.75)) 0 0;
    clip-path: polygon(0 0, 100% 0, 100% 30%, 70% 90%, 70% 100%, 30% 100%, 40% 90%, 0% 30%);
    top: v-bind(sizeFn(1));
    left: v-bind(sizeFn(2.8));
    width: v-bind(sizeFn(1.5));
    height: v-bind(sizeFn(2.5));
    transform-origin: 50% 30%;
  }

  .hamster__limb--br {
    animation: hamsterBRLimb var(--dur) linear infinite;
    background: linear-gradient(hsl(30, 90%, 80%) 90%, hsl(0, 90%, 75%) 90%);
    transform: rotate(-25deg) translateZ(-1px);
  }

  .hamster__limb--bl {
    animation: hamsterBLLimb var(--dur) linear infinite;
    background: linear-gradient(hsl(30, 90%, 90%) 90%, hsl(0, 90%, 85%) 90%);
    transform: rotate(-25deg);
  }

  .hamster__tail {
    animation: hamsterTail var(--dur) linear infinite;
    background: hsl(0, 90%, 85%);
    border-radius: v-bind(sizeFn(0.25)) 50% 50% v-bind(sizeFn(0.25));
    box-shadow: 0 v-bind(sizeFn(-0.2)) 0 hsl(0, 90%, 75%) inset;
    top: v-bind(sizeFn(1.5));
    right: v-bind(sizeFn(-0.5));
    width: v-bind(sizeFn(1));
    height: v-bind(sizeFn(0.5));
    transform: rotate(30deg) translateZ(-1px);
    transform-origin: v-bind(sizeFn(0.25)) v-bind(sizeFn(0.25));
  }

  .spoke {
    animation: spoke var(--dur) linear infinite;
    background:
      radial-gradient(100% 100% at center, hsl(0, 0%, 60%) 4.8%, hsla(0, 0%, 60%, 0) 5%),
      linear-gradient(hsla(0, 0%, 55%, 0) 46.9%, hsl(0, 0%, 65%) 47% 52.9%, hsla(0, 0%, 65%, 0) 53%) 50% 50% / 99% 99% no-repeat;
  }

  /* Animations */
  @keyframes hamster {
    from,
    to {
      transform: rotate(4deg) translate(v-bind(sizeFn(-0.8)), v-bind(sizeFn(1.85)));
    }

    50% {
      transform: rotate(0) translate(v-bind(sizeFn(-0.8)), v-bind(sizeFn(1.85)));
    }
  }

  @keyframes hamsterHead {
    from,
    25%,
    50%,
    75%,
    to {
      transform: rotate(0);
    }

    12.5%,
    37.5%,
    62.5%,
    87.5% {
      transform: rotate(8deg);
    }
  }

  @keyframes hamsterEye {
    from,
    90%,
    to {
      transform: scaleY(1);
    }

    95% {
      transform: scaleY(0);
    }
  }

  @keyframes hamsterEar {
    from,
    25%,
    50%,
    75%,
    to {
      transform: rotate(0);
    }

    12.5%,
    37.5%,
    62.5%,
    87.5% {
      transform: rotate(12deg);
    }
  }

  @keyframes hamsterBody {
    from,
    25%,
    50%,
    75%,
    to {
      transform: rotate(0);
    }

    12.5%,
    37.5%,
    62.5%,
    87.5% {
      transform: rotate(-2deg);
    }
  }

  @keyframes hamsterFRLimb {
    from,
    25%,
    50%,
    75%,
    to {
      transform: rotate(50deg) translateZ(-1px);
    }

    12.5%,
    37.5%,
    62.5%,
    87.5% {
      transform: rotate(-30deg) translateZ(-1px);
    }
  }

  @keyframes hamsterFLLimb {
    from,
    25%,
    50%,
    75%,
    to {
      transform: rotate(-30deg);
    }

    12.5%,
    37.5%,
    62.5%,
    87.5% {
      transform: rotate(50deg);
    }
  }

  @keyframes hamsterBRLimb {
    from,
    25%,
    50%,
    75%,
    to {
      transform: rotate(-60deg) translateZ(-1px);
    }

    12.5%,
    37.5%,
    62.5%,
    87.5% {
      transform: rotate(20deg) translateZ(-1px);
    }
  }

  @keyframes hamsterBLLimb {
    from,
    25%,
    50%,
    75%,
    to {
      transform: rotate(20deg);
    }

    12.5%,
    37.5%,
    62.5%,
    87.5% {
      transform: rotate(-60deg);
    }
  }

  @keyframes hamsterTail {
    from,
    25%,
    50%,
    75%,
    to {
      transform: rotate(30deg) translateZ(-1px);
    }

    12.5%,
    37.5%,
    62.5%,
    87.5% {
      transform: rotate(10deg) translateZ(-1px);
    }
  }

  @keyframes spoke {
    from {
      transform: rotate(0);
    }

    to {
      transform: rotate(-1turn);
    }
  }
</style>
